package com.booking.mainService.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.booking.login.pojo.User;
import com.booking.mainService.dao.MainDao;
import com.booking.mainService.pojo.Flight;
import com.booking.mainService.pojo.UserTicket;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapExecutor;

// TODO: Auto-generated Javadoc
/**
 * The Class MainDaoImpl.
 * 
 * @author coco
 * @version 1.0
 */
public class MainDaoImpl extends SqlMapClientDaoSupport implements MainDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#queryFlightList()
	 */
	public List queryFlightList() throws RuntimeException {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("queryFlightList");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#queryFlightById(int)
	 */
	public Flight queryFlightById(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		return (Flight) this.getSqlMapClientTemplate().queryForObject(
				"queryFlightById", id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#deleteFlightById(int)
	 */
	public void deleteFlightById(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().delete("deleteFlightById", id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.booking.mainService.dao.MainDao#insertFlight(com.booking.mainService
	 * .pojo.Flight)
	 */
	public void insertFlight(Flight flight) throws RuntimeException {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().insert("insertFlight", flight);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.booking.mainService.dao.MainDao#updateFlightById(com.booking.mainService
	 * .pojo.Flight)
	 */
	public void updateFlightById(Flight flight) throws RuntimeException {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("updateFlightById", flight);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#batchDelete(java.lang.String,
	 * java.lang.Object[])
	 */
	public void batchDelete(final String statementName, final Object[] o)
			throws RuntimeException {
		// TODO Auto-generated method stub
		// 参数数组为空或长度为0，则不执行任何操作
		if (o == null || o.length == 0) {
			// 参数数组长度为1，则直接执行一次操作
		} else if (o.length == 1) {
			this.getSqlMapClientTemplate().delete(statementName, o[0]);
			// 参数数组长度大于1，则使用批处理
		} else {
			this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
				public Object doInSqlMapClient(SqlMapExecutor executer)
						throws SQLException {
					// TODO Auto-generated method stub
					executer.startBatch();
					for (Object parameter : o) {
						executer.delete(statementName, parameter);
					}
					executer.executeBatch();
					return null;
				}
			});
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.booking.mainService.dao.MainDao#deleteFlights(java.lang.String[])
	 */
	public void deleteFlights(String[] id) throws RuntimeException {
		// TODO Auto-generated method stub
		Object[] idInt;
		if (id != null) {
			idInt = new Object[id.length];
			for (int i = 0; i < id.length; i++) {
				idInt[i] = Integer.parseInt(id[i]);
			}
			this.batchDelete("deleteFlightById", idInt);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.booking.mainService.dao.MainDao#queryFlightList(com.booking.mainService
	 * .pojo.Flight)
	 */
	public List queryFlightList(Flight flight) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList(
				"queryFlightListLikeInput", flight);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.booking.mainService.dao.MainDao#booking(com.booking.mainService.pojo
	 * .UserTicket)
	 */
	public String booking(UserTicket userTicket) throws RuntimeException,
			SQLException {
		// TODO Auto-generated method stub
		String str = null;
		SqlMapClient sqlMapClient = this.getSqlMapClient();
		try {
			// 开始事务
			sqlMapClient.startTransaction();
			// 查询航班
			Flight flight = (Flight) sqlMapClient.queryForObject(
					"queryFlightById", userTicket.getFlightId());
			// 如果没有该航班或者该航班剩余票数小于订票数
			if (flight != null) {
				if (flight.getLeftNum() >= userTicket.getNum()) {
					// 插入订票
					sqlMapClient.insert("insertUserTicket", userTicket);
					// 航班剩余票数-订票数
					sqlMapClient.update("updateFlightLeftNumById", userTicket);
					// 提交事务
					sqlMapClient.commitTransaction();
				} else {
					str = "noEnoughTicket";
				}
			} else {
				str = "noFlight";
			}
		} finally {
			// 结束事务
			sqlMapClient.endTransaction();
		}
		return str;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#queryUserTicketListByFlightId
	 * (int)
	 */
	public List queryUserTicketListByFlightId(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList(
				"queryUserTicketListByFlightId", id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#queryUserTicketListByUserId
	 * (int)
	 */
	public List queryUserTicketListByUserId(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList(
				"queryUserTicketListByUserId", id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#queryUserTicketById(int)
	 */
	public UserTicket queryUserTicketById(int id) throws RuntimeException {
		return (UserTicket) this.getSqlMapClientTemplate().queryForObject(
				"queryUserTicketById", id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#updateUserTicketById(com.
	 * booking.mainService.pojo.UserTicket)
	 */
	public String updateUserTicketById(UserTicket userTicket)
			throws RuntimeException, SQLException {
		// TODO Auto-generated method stub
		String str = null;
		SqlMapClient sqlMapClient = this.getSqlMapClient();
		try {
			// 开始事务
			sqlMapClient.startTransaction();
			// 查询当前定了多少票
			UserTicket userTicket1 = this.queryUserTicketById(userTicket
					.getId());
			if (userTicket1 != null) {
				// 查询航班
				Flight flight = (Flight) sqlMapClient.queryForObject(
						"queryFlightById", userTicket.getFlightId());
				// 如果没有该航班或者该航班剩余票数小于订票数
				if (flight != null) {
					// 相差多少票
					int addNum = userTicket.getNum() - userTicket1.getNum();
					if (flight.getLeftNum() >= addNum) {
						// 更新订票
						sqlMapClient.update("updateUserTicketById", userTicket);
						userTicket.setNum(addNum);
						// 航班剩余票数-订票数
						sqlMapClient.update("updateFlightLeftNumById",
								userTicket);
						// 提交事务
						sqlMapClient.commitTransaction();
					} else {
						str = "noEnoughTicket";
					}
				} else {
					str = "noFlight";
				}
			} else {
				str = "noUserTicket";
			}
		} finally {
			// 结束事务
			sqlMapClient.endTransaction();
		}
		return str;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#deleteUserTicketById(com.
	 * booking.mainService.pojo.UserTicket)
	 */
	public String deleteUserTicketById(UserTicket userTicket)
			throws RuntimeException, SQLException {
		// TODO Auto-generated method stub
		String str = null;
		SqlMapClient sqlMapClient = this.getSqlMapClient();
		try {
			// 开始事务
			sqlMapClient.startTransaction();
			// 查询当前定了多少票
			UserTicket userTicket1 = this.queryUserTicketById(userTicket
					.getId());
			if (userTicket1 != null) {
				// 查询航班
				Flight flight = (Flight) sqlMapClient.queryForObject(
						"queryFlightById", userTicket.getFlightId());
				// 如果没有该航班或者该航班剩余票数小于订票数
				if (flight != null) {
					// 删除订票
					sqlMapClient.delete("deleteUserTicketById", userTicket);
					userTicket.setNum(0 - userTicket.getNum());
					// 航班剩余票数-订票数
					sqlMapClient.update("updateFlightLeftNumById", userTicket);
					// 提交事务
					// sqlMapClient.commitTransaction();
				} else {
					str = "noFlight";
				}
			} else {
				str = "noUserTicket";
			}
		} finally {
			// 结束事务
			sqlMapClient.endTransaction();
		}
		return str;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#queryUserList()
	 */
	public List queryUserList() throws RuntimeException {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("queryUserList");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#queryUserById(int)
	 */
	public User queryUserById(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		return (User) this.getSqlMapClientTemplate().queryForObject(
				"queryUserById", id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#deleteUserById(int)
	 */
	public void deleteUserById(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().delete("deleteUserById", id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.booking.mainService.dao.MainDao#insertUser(com.booking.
	 * mainService.pojo.User)
	 */
	public void insertUser(User user) throws RuntimeException {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().insert("insertUser", user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#updateUserById(com.booking
	 * .mainService.pojo.User)
	 */
	public void updateUserById(User user) throws RuntimeException {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("updateUserById", user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.booking.mainService.dao.MainDao#deleteUsers(java.lang.String [])
	 */
	public void deleteUsers(String[] id) throws RuntimeException {
		// TODO Auto-generated method stub
		Object[] idInt;
		if (id != null) {
			idInt = new Object[id.length];
			for (int i = 0; i < id.length; i++) {
				idInt[i] = Integer.parseInt(id[i]);
			}
			this.batchDelete("deleteUserById", idInt);
		}
	}

}
